{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-2-911ded80008e>:2: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please write your own downloading logic.\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From D:\\SoftWare\\Anaconda3\\envs\\pyDL\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n"
     ]
    }
   ],
   "source": [
    "# 载入数据\n",
    "mnist = input_data.read_data_sets('MNIST_data', one_hot=True)s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter 0 Testing Accuracy: 0.882\n",
      "Iter 1 Testing Accuracy: 0.9054\n",
      "Iter 2 Testing Accuracy: 0.916\n",
      "Iter 3 Testing Accuracy: 0.9242\n",
      "Iter 4 Testing Accuracy: 0.9294\n",
      "Iter 5 Testing Accuracy: 0.9344\n",
      "Iter 6 Testing Accuracy: 0.937\n",
      "Iter 7 Testing Accuracy: 0.9385\n",
      "Iter 8 Testing Accuracy: 0.9414\n",
      "Iter 9 Testing Accuracy: 0.9442\n",
      "Iter 10 Testing Accuracy: 0.9458\n",
      "Iter 11 Testing Accuracy: 0.9469\n",
      "Iter 12 Testing Accuracy: 0.949\n",
      "Iter 13 Testing Accuracy: 0.9501\n",
      "Iter 14 Testing Accuracy: 0.9508\n",
      "Iter 15 Testing Accuracy: 0.9512\n",
      "Iter 16 Testing Accuracy: 0.9526\n",
      "Iter 17 Testing Accuracy: 0.9535\n",
      "Iter 18 Testing Accuracy: 0.9541\n",
      "Iter 19 Testing Accuracy: 0.9559\n",
      "Iter 20 Testing Accuracy: 0.9571\n",
      "Iter 21 Testing Accuracy: 0.9575\n",
      "Iter 22 Testing Accuracy: 0.9579\n",
      "Iter 23 Testing Accuracy: 0.9587\n",
      "Iter 24 Testing Accuracy: 0.9589\n",
      "Iter 25 Testing Accuracy: 0.96\n",
      "Iter 26 Testing Accuracy: 0.9606\n",
      "Iter 27 Testing Accuracy: 0.9611\n",
      "Iter 28 Testing Accuracy: 0.962\n",
      "Iter 29 Testing Accuracy: 0.9624\n",
      "Iter 30 Testing Accuracy: 0.9622\n",
      "Iter 31 Testing Accuracy: 0.9628\n",
      "Iter 32 Testing Accuracy: 0.9634\n",
      "Iter 33 Testing Accuracy: 0.9639\n",
      "Iter 34 Testing Accuracy: 0.9641\n",
      "Iter 35 Testing Accuracy: 0.9649\n",
      "Iter 36 Testing Accuracy: 0.9648\n",
      "Iter 37 Testing Accuracy: 0.9653\n",
      "Iter 38 Testing Accuracy: 0.9656\n",
      "Iter 39 Testing Accuracy: 0.9662\n",
      "Iter 40 Testing Accuracy: 0.9665\n",
      "Iter 41 Testing Accuracy: 0.9673\n",
      "Iter 42 Testing Accuracy: 0.968\n",
      "Iter 43 Testing Accuracy: 0.9678\n",
      "Iter 44 Testing Accuracy: 0.9679\n",
      "Iter 45 Testing Accuracy: 0.9686\n",
      "Iter 46 Testing Accuracy: 0.968\n",
      "Iter 47 Testing Accuracy: 0.9688\n",
      "Iter 48 Testing Accuracy: 0.9692\n",
      "Iter 49 Testing Accuracy: 0.9692\n",
      "Iter 50 Testing Accuracy: 0.9699\n",
      "Iter 51 Testing Accuracy: 0.9698\n",
      "Iter 52 Testing Accuracy: 0.9702\n",
      "Iter 53 Testing Accuracy: 0.9699\n",
      "Iter 54 Testing Accuracy: 0.9703\n",
      "Iter 55 Testing Accuracy: 0.9706\n",
      "Iter 56 Testing Accuracy: 0.9711\n",
      "Iter 57 Testing Accuracy: 0.9712\n",
      "Iter 58 Testing Accuracy: 0.9715\n",
      "Iter 59 Testing Accuracy: 0.9711\n",
      "Iter 60 Testing Accuracy: 0.9714\n",
      "Iter 61 Testing Accuracy: 0.9722\n",
      "Iter 62 Testing Accuracy: 0.9717\n",
      "Iter 63 Testing Accuracy: 0.9716\n",
      "Iter 64 Testing Accuracy: 0.9725\n",
      "Iter 65 Testing Accuracy: 0.9725\n",
      "Iter 66 Testing Accuracy: 0.9725\n",
      "Iter 67 Testing Accuracy: 0.9727\n",
      "Iter 68 Testing Accuracy: 0.9728\n",
      "Iter 69 Testing Accuracy: 0.9727\n",
      "Iter 70 Testing Accuracy: 0.9729\n",
      "Iter 71 Testing Accuracy: 0.9735\n",
      "Iter 72 Testing Accuracy: 0.9733\n",
      "Iter 73 Testing Accuracy: 0.9741\n",
      "Iter 74 Testing Accuracy: 0.9736\n",
      "Iter 75 Testing Accuracy: 0.9735\n",
      "Iter 76 Testing Accuracy: 0.9738\n",
      "Iter 77 Testing Accuracy: 0.9743\n",
      "Iter 78 Testing Accuracy: 0.974\n",
      "Iter 79 Testing Accuracy: 0.9744\n",
      "Iter 80 Testing Accuracy: 0.9747\n",
      "Iter 81 Testing Accuracy: 0.9752\n",
      "Iter 82 Testing Accuracy: 0.9751\n",
      "Iter 83 Testing Accuracy: 0.9745\n",
      "Iter 84 Testing Accuracy: 0.975\n",
      "Iter 85 Testing Accuracy: 0.9749\n",
      "Iter 86 Testing Accuracy: 0.9753\n",
      "Iter 87 Testing Accuracy: 0.9756\n",
      "Iter 88 Testing Accuracy: 0.9752\n",
      "Iter 89 Testing Accuracy: 0.9761\n",
      "Iter 90 Testing Accuracy: 0.9759\n",
      "Iter 91 Testing Accuracy: 0.9764\n",
      "Iter 92 Testing Accuracy: 0.9768\n",
      "Iter 93 Testing Accuracy: 0.9764\n",
      "Iter 94 Testing Accuracy: 0.9766\n",
      "Iter 95 Testing Accuracy: 0.977\n",
      "Iter 96 Testing Accuracy: 0.9768\n",
      "Iter 97 Testing Accuracy: 0.9771\n",
      "Iter 98 Testing Accuracy: 0.977\n",
      "Iter 99 Testing Accuracy: 0.9767\n"
     ]
    }
   ],
   "source": [
    "# 批次的大小\n",
    "batch_size = 128\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "x = tf.placeholder(tf.float32, [None,784])\n",
    "y = tf.placeholder(tf.float32, [None, 10])\n",
    "keep_prob = tf.placeholder(tf.float32)\n",
    "\n",
    "# 创建神经网络\n",
    "W1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1))\n",
    "b1 = tf.Variable(tf.zeros([1, 2000]))\n",
    "# 激活层\n",
    "layer1 = tf.nn.relu(tf.matmul(x,W1) + b1)\n",
    "# drop层\n",
    "layer1 = tf.nn.dropout(layer1,keep_prob=keep_prob)\n",
    "\n",
    "# 第二层\n",
    "W2 = tf.Variable(tf.truncated_normal([2000,500],stddev=0.1))\n",
    "b2 = tf.Variable(tf.zeros([1, 500]))\n",
    "layer2 = tf.nn.relu(tf.matmul(layer1,W2) + b2)\n",
    "layer2 = tf.nn.dropout(layer2,keep_prob=keep_prob)\n",
    "\n",
    "# 第三层\n",
    "W3 = tf.Variable(tf.truncated_normal([500,10],stddev=0.1))\n",
    "b3 = tf.Variable(tf.zeros([1,10]))\n",
    "prediction = tf.nn.sigmoid(tf.matmul(layer2,W3) + b3)\n",
    "\n",
    "loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))\n",
    "\n",
    "# 梯度下降法\n",
    "# train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)#得到97的正确率\n",
    "train_step = tf.train.AdadeltaOptimizer(0.1).minimize(loss)\n",
    "\n",
    "\n",
    "# 初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "prediction_2 = tf.nn.softmax(prediction)\n",
    "# 得到一个布尔型列表，存放结果是否正确\n",
    "correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction_2,1)) #argmax 返回一维张量中最大值索引\n",
    "\n",
    "# 求准确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32)) # 把布尔值转换为浮点型求平均数\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    for epoch in range(100):\n",
    "        for batch in range(n_batch):\n",
    "            # 获得批次数据\n",
    "            batch_xs, batch_ys = mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step, feed_dict={x:batch_xs, y:batch_ys, keep_prob:0.8})\n",
    "        acc = sess.run(accuracy, feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0} )\n",
    "        print(\"Iter \" + str(epoch) + \" Testing Accuracy: \" + str(acc))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
